<?php
class omeauto_ctl_autoconfirm extends omeauto_controller{
    var $workground = "setting_tools";
    
    function index(){
        $params = array(
            'title'=>'订单自动确认规则',
            'actions' => array(
                array(
                    'label'=>'新建',
                    'href'=>'index.php?app=omeauto&ctl=autoconfirm&act=add',
                    'target'=>'_blank'
                )
            ),
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>true,
            'use_buildin_export'=>false,
            'use_buildin_import'=>false,
            'use_buildin_filter'=>false,
            'use_view_tab'=>false,
       );
        $this->finder('omeauto_mdl_autoconfirm',$params);
    }
    

    
    function add(){
        $this->_edit();
    }
    
    function edit($oid){
        $this->_edit($oid);
    }
    
    private function _edit($oid=NULL){
        $conditions = kernel::single('omeauto_autoconfirm')->get_conditions();
        
        if($oid){
            $oConfirm = &$this->app->model("autoconfirm");
            $autoconfirm = $oConfirm->dump($oid);
            $this->pagedata['autoconfirm'] = $autoconfirm;
            
            if($autoconfirm['config']){
                $condition_detail = "";
                $i = 0;
                foreach($autoconfirm['config'] as $condition=>$detail){
                    foreach($detail as $k=>$v){
                        $condition_detail[$i]['title'] = kernel::single($condition)->name;
                        $condition_detail[$i]['content'] = kernel::single('omeauto_auto')->get_condition_detail($condition,$v);
                        $condition_detail[$i]['condition'] = $condition;
                        $i++;
                        
                        if(isset($conditions[$condition])){
                            $conditions[$condition]['disabled'] = true;
                        }
                    }
                }
                $this->pagedata['condition_detail'] = $condition_detail;
            }
        }
        
        $this->pagedata['branch_condition'] = $autoconfirm['branch']?$autoconfirm['branch']:'omeauto_autoconfirm_branch_weight';
        $this->pagedata['dlycorp_condition'] = $autoconfirm['dlycorp']?$autoconfirm['dlycorp']:'omeauto_autoconfirm_dlycorp_weight';
        
        foreach(kernel::servicelist("autoconfirm_branch") as $object){
            $branch_condition = get_class($object);
            $this->pagedata['branch_condition_list'][$branch_condition] = $object->name; 
        }
        
        foreach(kernel::servicelist("autoconfirm_dlycorp") as $object){
            $dlycorp_condition = get_class($object);
            $this->pagedata['dlycorp_condition_list'][$dlycorp_condition] = $object->name; 
        }
        
        $this->pagedata['conditions'] = $conditions;
		$this->pagedata['title'] = '订单自动确认规则添加/编辑';
        $this->singlepage('autoconfirm/index.html');
    }
    
    function do_add(){
        $this->begin("index.php?app=omeauto&ctl=autoconfirm&act=index");
        $data = $_POST;
        $oAutoconfirm = &$this->app->model('autoconfirm');
        $oAutoconfirm->save($data);
        $this->end(true,'保存成功');
    }
}
?>
